﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Roles
{
    public partial class manageSingleRole : SecurityPage
    {
        private const string DATA_SOURCE = "WebAdminDataSource";

        public void SetDataSource(object v)
        {
            Session[DATA_SOURCE] = v;
        }

        public void BindGrid(bool displayUsersNotFound)
        {
            dataGrid.DataSource = Session[DATA_SOURCE];
            dataGrid.DataBind();
            if (dataGrid.Rows.Count == 0)
            {
                if (displayUsersNotFound)
                {
                    notFoundUsers.Visible = true;
                }
            }
        }

        public void IndexChanged(object sender, GridViewPageEventArgs e)
        {
            dataGrid.PageIndex = e.NewPageIndex;
            BindGrid(false);
        }

        public void EnabledChanged(object sender, EventArgs e)
        {
            var checkBox = (CheckBox) sender;
            var gvr = (GridViewRow) checkBox.Parent.Parent;
            var label = (Label) gvr.FindControl("userNameLink");
            string userName = label.Text;
            string currentRoleName = CurrentRole;
            if (checkBox.Checked)
            {
                if (
                    !(bool)
                     CallWebAdminHelperMethod(false, "IsUserInRole", new object[] {userName, currentRoleName},
                                              new[] {typeof (string), typeof (string)}))
                {
                    CallWebAdminHelperMethod(false, "AddUsersToRoles",
                                             new object[] {new[] {userName}, new[] {currentRoleName}},
                                             new[] {typeof (string[]), typeof (string[])});
                }
            }
            else
            {
                if (
                    (bool)
                    CallWebAdminHelperMethod(false, "IsUserInRole", new object[] {userName, currentRoleName},
                                             new[] {typeof (string), typeof (string)}))
                {
                    CallWebAdminHelperMethod(false, "RemoveUsersFromRoles",
                                             new object[] {new[] {userName}, new[] {currentRoleName}},
                                             new[] {typeof (string[]), typeof (string[])});
                }
            }
        }

        public void Page_Load()
        {
            if (!IsPostBack)
            {
                PopulateRepeaterDataSource();
                repeater.DataBind();
                string currentRoleName = CurrentRole;
                roleName.Text = currentRoleName;

                var muc =
                    (string[])
                    CallWebAdminHelperMethod(false, "GetUsersInRole", new object[] {currentRoleName},
                                             new[] {typeof (string)});
                var Coll = new MembershipUserCollection();
                // no Role method for returning a MembershipUserCollection.
                MembershipUser OneUser = null;
                foreach (string username in muc)
                {
                    try
                    {
                        OneUser =
                            (MembershipUser)
                            CallWebAdminHelperMethod(true, "GetUser", new object[] {username, false /* isOnline */},
                                                     new[] {typeof (string), typeof (bool)});
                    }
                    catch
                    {
                        // eat it
                    }
                    if (OneUser != null)
                    {
                        Coll.Add(OneUser);
                    }
                }
                SetDataSource(Coll);
                BindGrid(false);
                dataGrid.HeaderStyle.HorizontalAlign = DirectionalityHorizontalAlign;
            }
        }


        private void PopulateRepeaterDataSource()
        {
            PopulateRepeaterDataSource(repeater);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (dataGrid.Rows.Count == 0)
            {
                containerTable.Visible = false;
            }
            else
            {
                containerTable.Visible = true;
            }
        }

        public void RedirectToAddUser(object sender, EventArgs e)
        {
            // do not prepend ~/ to this path since it is not at the root
            Response.Redirect("adduser.aspx");
        }

        public void RetrieveLetter(object sender, RepeaterCommandEventArgs e)
        {
            RetrieveLetter(sender, e, dataGrid, (string) GetLocalResourceObject("All"));
            SetDataSource(dataGrid.DataSource);
            BindGrid(true);
        }

        public void SearchForUsers(object sender, EventArgs e)
        {
            SearchForUsers(sender, e, repeater, dataGrid, dropDown1, textBox1);
            SetDataSource(dataGrid.DataSource);
            BindGrid(true);
            //multiView1.ActiveViewIndex = 0;
        }
    }
}